<?php

App::uses('AppController', 'Controller');

/**
 * Backups Controller
 *
 * @property Backup $Backup
 */
class BackupsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->paginate = array('limit' => $this->Backup->find('count'));
        $this->Backup->recursive = 0;
        $this->set('backups', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Backup->exists($id)) {
            throw new NotFoundException('Invalid backup','default',array('class'=>'alert alert-danger'));
        }
        $options = array('conditions' => array('Backup.' . $this->Backup->primaryKey => $id));
        $this->set('backup', $this->Backup->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Backup->create();
            if ($this->Backup->save($this->request->data)) {
                $this->Session->setFlash('The backup has been saved','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The backup could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Backup->exists($id)) {
            throw new NotFoundException('Invalid backup','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Backup->save($this->request->data)) {
                $this->Session->setFlash('The backup has been saved','default',array('class'=>'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The backup could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Backup.' . $this->Backup->primaryKey => $id));
            $this->request->data = $this->Backup->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Backup->id = $id;
        if (!$this->Backup->exists()) {
            throw new NotFoundException(__('Invalid backup'));
        }
        $this->request->onlyAllow('post', 'delete');
        
        $backup = $this->Backup->find('first',array('conditions' => array('id' => $id)));
        
        unlink($backup['Backup']['path']);
        
        
        if ($this->Backup->delete()) {
            $this->Session->setFlash('Backup deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Backup was not deleted','default',array('class'=>'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function backup() {
        $dbhost = 'localhost';
        $dbuser = 'DcabUser';
        $dbpassword = 'dcab';
        $db = 'dcabdb';
        $date = $this->request->query('date');
        $time = $this->request->query('time');
        $timepath = $this->request->query('timepath');
        $sqldumppath = 'C:\wamp\bin\mysql\mysql5.5.24\bin\mysqldump';
        $backupfile = 'C:\wamp\www\DCAB\backup\backup-' . $date.$timepath . '.sql';

        exec($sqldumppath . ' --user=' . $dbuser . ' --password=' . $dbpassword . ' --host=' . $dbhost . ' ' . $db . ' > ' . $backupfile);

        $this->Backup->set('name', 'Backup-' . $date.$time);
        $this->Backup->set('date', $date.' '.$time);
        $this->Backup->set('path', $backupfile);
        if ($this->Backup->saveAll()) {
            $this->Session->setFlash('The database has been saved', 'default', array('class' => 'alert alert-info'));
        } else {
            $this->Session->setFlash('The database could not be saved', 'default', array('class' => 'alert alert-danger'));
        }
        $this->redirect('index');
    }
    
    public function restore($id = null){
        $backup = $this->Backup->find('first',array('conditions' => array('id' => $id)));
        
        $dbhost = 'localhost';
        $dbuser = 'DcabUser';
        $dbpassword = 'dcab';
        $db = 'dcabdb';
        $backupfile = $backup['Backup']['path'];
        $sqldumppath = 'C:\wamp\bin\mysql\mysql5.5.24\bin\mysql';
        
         exec($sqldumppath . ' --user=' . $dbuser . ' --password=' . $dbpassword . ' --host=' . $dbhost . ' ' . $db . ' < ' . $backupfile);
         
         $this->Session->setFlash('The database '.$backup['backups']['name'].' has been restored', 'default', array('class' => 'alert alert-info'));
         
         $this->redirect('index');
    }

}
